package sparsing;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class Assignment implements Serializable {

	private static final long serialVersionUID = -7370553192903276439L;

	private transient int _id;
	private int _deletedID;
	private int _assignedID;
	private double _km;
	private short _direction;
	private short _factor;
	private short _typ;

	public Assignment() {
		this(0, 0, 0, 0, (short) 0, (short) 0, (short) 0);
	}

	public Assignment(int id, int deletedID, int assignedID, double km,
			short direction, short factor, short typ) {
		_id = id;
		_deletedID = deletedID;
		_assignedID = assignedID;
		_km = km;
		_direction = direction;
		_factor = factor;
		_typ = typ;
	}

	public int getID() {
		return _id;
	}

	public void setID(int id) {
		this._id = id;
	}

	public int getDeletedID() {
		return _deletedID;
	}

	public void setDeletedID(int deletedID) {
		this._deletedID = deletedID;
	}

	public int getAssignedID() {
		return _assignedID;
	}

	public void setAssignedID(int assignedID) {
		this._assignedID = assignedID;
	}

	public double getKm() {
		return _km;
	}

	public void setKm(double km) {
		this._km = km;
	}

	public short getDirection() {
		return _direction;
	}

	public void setDirection(short direction) {
		this._direction = direction;
	}

	public short getFactor() {
		return _factor;
	}

	public void setFactor(short factor) {
		this._factor = factor;
	}

	public short getTyp() {
		return _typ;
	}

	public void setTyp(short typ) {
		this._typ = typ;
	}

	public Assignment getCoAssignment(Connection con) {

		Assignment res = new Assignment();

		String sql = new String();
		sql += "SELECT * FROM assignment WHERE DeletedID = " + _deletedID
				+ " AND ID != " + _id;

		Statement stmt = null;
		ResultSet rs = null;

		try {

			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);

			int i = 0;
			while (rs.next()) {

				int id = rs.getInt("ID");
				int deletedID = rs.getInt("DeletedID");
				int assignedID = rs.getInt("AssignedID");
				double km = rs.getInt("Km");
				short direction = rs.getShort("Richtung");
				short factor = rs.getShort("FaktorPKW");
				short typ = rs.getShort("RoadClass");

				res.setID(id);
				res.setDeletedID(deletedID);
				res.setAssignedID(assignedID);
				res.setKm(km);
				res.setDirection(direction);
				res.setFactor(factor);
				res.setTyp(typ);

				if (i > 0)
					System.out.println("FEHLER! getCoAssignment");

				i++;

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}

		return res;
	}

	public static List<Assignment> getAllAssignments(Connection con, int node) {

		List<Assignment> list = new LinkedList<Assignment>();

		String sql = new String();
		sql += "SELECT * FROM assignment WHERE AssignedID = " + node;

		Statement stmt = null;
		ResultSet rs = null;

		try {

			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);

			while (rs.next()) {

				int id = rs.getInt("ID");
				int deletedID = rs.getInt("DeletedID");
				int assignedID = rs.getInt("AssignedID");
				double km = rs.getInt("Km");
				short direction = rs.getShort("Richtung");
				short factor = rs.getShort("FaktorPKW");
				short typ = rs.getShort("RoadClass");

				list.add(new Assignment(id, deletedID, assignedID, km,
						direction, factor, typ));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}

		return list;
	}

	public void updateAssignment(Connection con) {

		String sql = new String();
		sql += "UPDATE assignment SET assignedID = " + _assignedID + ",Km = "
				+ _km + ",Richtung = " + _direction + " WHERE ID = " + _id;

		Statement stmt = null;

		try {

			stmt = con.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}
	}

	public static void insertAssignments(Connection con, Assignment a1,
			Assignment a2) {

		String sql = new String();
		sql += "INSERT INTO assignment "
				+ "(DeletedID,AssignedID,Km,RoadClass,FaktorPKW,Richtung)"
				+ " VALUES (" + a1.getDeletedID() + "," + a1.getAssignedID()
				+ "," + a1.getKm() + "," + a1.getTyp() + "," + a1.getFactor()
				+ "," + a1.getDirection() + ")";
		sql += ",(" + a2.getDeletedID() + "," + a2.getAssignedID() + ","
				+ a2.getKm() + "," + a2.getTyp() + "," + a2.getFactor() + ","
				+ a2.getDirection() + ")";

		Statement stmt = null;

		try {

			stmt = con.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}

	}

}
